import { defineStore } from "pinia";
import TypeSum from "@/TypeSum/entry";
import request from '@/API/entry'
import { getUserName } from '@/tools/local'
type Datas = TypeSum['Global']['globalStoreData']

export const globalStore = defineStore('globalStore', {
    state: (): Datas => {
        return {
            userName: ''
        }
    },
    actions: {
        async getUserData() {
            const result = await request.ApiRequest.reqUserData() as TypeSum['AxiosData']['AxiosData']
            if (result.code == 200) {
                this.userName = getUserName()
                return true
            } else {
                localStorage.removeItem('name')
                localStorage.removeItem('nickName')
                localStorage.removeItem('token')
                localStorage.removeItem('userId')
                this.userName = ''
                return false
            }
        },
        async getQuitUser() {
            const result = await request.ApiRequest.reqQuitLogin() as TypeSum['AxiosData']['AxiosData']
            if (result.code == 200) {
                this.getUserData()
                return true
            } else {
                return false
            }
        }
    }
})